#!/usr/bin/perl -w
##
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
# -- 06/23/2009 Created by Zhixin Li
# -- Used to analyze mysql starting configuration
# -- check mysqld option : skip-name-resolve,user,skip-grant-tables
# -- changed output from key|value to value|value|value, 06/26/2009  modified by Zhixin Li
use strict;
my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}

my $ostype = `/bin/uname`;
my $SQL1="show variables like 'pid_file'";
my $pidfile=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $file = substr($pidfile,index($pidfile,"\t")+1,length($pidfile)-index($pidfile,"\t")+1);
my $mypid;
my $proc;
chomp($file);
open (FILE, "$file") or die "Couldn't open $file for reading: $!\n"; 
$/=""; 
$mypid = <FILE>; 
close(FILE); 
$/="\n";
chomp($mypid);
my $x;
my $y;
my @z;
my @k;
my $mydef;
my $count1;
my $count2;
my $count3;
###################################################################
#  column:  v_skipname   v_skipgrant   v_runuser
#  value:    $count1       $count2      $count3  0 nonexist,1 exist
###################################################################
chomp($ostype);
if ($ostype eq "SunOS") {
   $proc =`/usr/ucb/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));
   $y = grep(/--defaults-file/,$x);
   if ($y == 0) {
      if (-s '/etc/my.cnf') {
         print "em_result=0${DELIM}0${DELIM}0${DELIM}0\n";
         exit 0;
      } else { 
         $mydef =`$MYSQL_HOME/bin/mysqld --print-defaults|/bin/grep -v arguments`;
         $count1 = grep(/--skip-name-resolve/,$mydef);
         $count2 = grep(/--skip-grant-tables/,$mydef);
         $count3 = grep(/--user=root/,$mydef);
         print "em_result=$count1${DELIM}$count2${DELIM}$count3\n";
         exit 0;
      }
   }  else {
      @z = split (/ +/, $x);
      foreach (@z) {
         if (/^--defaults-file/) {
            $mydef = `$MYSQL_HOME/bin/mysqld $_ --print-defaults|/bin/grep -v arguments`;
            $count1 = grep(/--skip-name-resolve/,$mydef);
            $count2 = grep(/--skip-grant-tables/,$mydef);
            $count3 = grep(/--user=root/,$mydef);
            print "em_result=$count1${DELIM}$count2${DELIM}$count3\n";
            exit 0;
         }
      }
   }
} else {
   $proc =`/bin/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));
   $y = grep(/--defaults-file=/,$x);
   if ($y == 0) {
         if (-s '/etc/my.cnf') {
         print "em_result=0${DELIM}0${DELIM}0${DELIM}0\n";
         exit 0;
      } else {
         $mydef = `$MYSQL_HOME/bin/mysqld --print-defaults|/bin/grep -v arguments`;
         $count1 = grep(/--skip-name-resolve/,$mydef);
         $count2 = grep(/--skip-grant-tables/,$mydef);   
         $count3 = grep(/--user=root/,$mydef);
         print "em_result=$count1${DELIM}$count2${DELIM}$count3\n";
         exit 0;
      }
   }  else {
        @z = split (/ +/, $x);
        foreach (@z) {
           if (/^--defaults-file/) {
              $mydef = `$MYSQL_HOME/bin/mysqld $_ --print-defaults|/bin/grep -v arguments`;
              $count1 = grep(/--skip-name-resolve/,$mydef);
              $count2 = grep(/--skip-grant-tables/,$mydef); 
              $count3 = grep(/--user=root/,$mydef);
              print "em_result=$count1${DELIM}$count2${DELIM}$count3\n";
              exit 0;
           }
      }
   }
}
